package com.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.domain.PageResult;
import com.basic.util.ConverterUtil;
import com.basic.util.QueryHelpPlus;
import com.demo.mapper.ProductMapper;
import com.demo.pojo.Product;
import com.demo.service.IProductService;
import com.demo.service.criteria.ProductQueryCriteria;
import com.demo.service.dto.ProductDto;
import com.demo.service.vo.MyConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ProductDto saveOrUpdate(ProductDto productDto) {
        final Product product = ConverterUtil.convert(productDto, Product.class);
        if (ObjectUtil.isNull(product.getId())) {
            baseMapper.insert(product);
        } else {
            baseMapper.updateById(product);
        }
        return convertDto(product, null);
    }

    @Override
    public List<ProductDto> queryAll(ProductQueryCriteria criteria) {
        return convertDto(baseMapper.selectList(query(criteria)), criteria);
    }

    @Override
    public PageResult<ProductDto> queryAll(ProductQueryCriteria criteria, Pageable pageable) {
        final int page;
        final int limit;
        if (criteria.getPage() != null && criteria.getLimit() != null) {
            page = criteria.getPage();
            limit = criteria.getLimit();
        } else {
            page = pageable.getPageNumber();
            limit = pageable.getPageSize();
        }
        final IPage<Product> pageList = baseMapper.selectPage(new Page<>(page, limit), query(criteria));

        return new PageResult<ProductDto>(pageList.getTotal(), convertDto(pageList.getRecords(), criteria));

    }

    @Override
    public ProductDto queryById(Long id) {
        final List<ProductDto> records = queryAll(ProductQueryCriteria.builder().id(id).build());
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public ProductDto queryOne(ProductQueryCriteria criteria) {
        final List<ProductDto> records = queryAll(criteria);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public Long count(ProductQueryCriteria criteria) {
        return baseMapper.selectCount(query(criteria));
    }

    @Override
    public Product getOne(ProductQueryCriteria criteria) {
        final List<Product> records = getAll(criteria);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public List<Product> getAll(ProductQueryCriteria criteria) {
        return baseMapper.selectList(query(criteria));
    }

    @Override
    public int deleteId(Long id) {
        final ProductDto ProductDto = queryById(id);
        delete(ProductDto);
        return 1;
    }

    @Override
    @Transactional
    public int deleteByMore(ProductQueryCriteria criteria) {
        final List<ProductDto> ProductDtos = queryAll(criteria);
        for (ProductDto each: ProductDtos) {
            delete(each);
        }
        return ProductDtos.size();
    }

    private void delete(ProductDto productDto) {
        productDto.setIsShow(0);
        saveOrUpdate(productDto);
    }

    @Override
    public List<Product> testXml() {
        return productMapper.selectAllData();
    }

    /**
     * 后续所有的sql逻辑都在这里处理
     */
    private QueryWrapper<Product> query(ProductQueryCriteria criteria) {
        final QueryWrapper<Product> wrapper = QueryHelpPlus.getPredicate(Product.class, criteria);
        if (ObjectUtil.isNull(criteria)) {
            return wrapper;
        }

        if (ObjectUtil.isNull(criteria.getIsShow())) {
            wrapper.eq("is_show", 1);
        }

        wrapper.apply(StrUtil.format("((id > {} AND id IN ({})) OR (id < {} AND id <> {}))", 1, 2, 100, 0));

        return wrapper;
    }

    private ProductDto convertDto(Product record, ProductQueryCriteria criteria) {
        return MyConverterUtils.INSTANCE.toProductDto(record);
    }

    private List<ProductDto> convertDto(List<Product> records, ProductQueryCriteria criteria) {

        final List<ProductDto> result = new ArrayList<>();

        for (Product each : records) {
            result.add(convertDto(each, criteria));
        }

        return result;
    }
}
